#include<iostream>
using namespace std;
const int N = 1e6 + 10;
int f[N];
int fun2(int x)
{
	int ans = 0;
	while (x) {
		int k = x % 10;
		if (k != 0) {
			ans++;
		}
		x /= 10;
	}
	return ans == 1 ? 1 : 0;
}
void fun()
{
	for (int i = 1; i < N; i++) {
		f[i] = f[i - 1] + fun2(i);
	}
}
int main()
{
	int t;
	cin >> t;
	int x;
	fun();
	while (t--) {
		cin >> x;
		cout << f[x] << endl;
	}
	return 0;
}